using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class Shop_ProductAttributes
	{
		private Conn conn;
		public Shop_ProductAttributes()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public Shop_ProductAttributes(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAllAttributes()
		{
			return this.conn.GetDataTable("SELECT * From [HLShop_ProductAttributes]");
		}
		public bool UpdateAttributes(string AttributesName, string AttributesValue, int Id, string AttributesMethods, string AttributesSelect)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Attributes_Name", DbType.String, AttributesName),
				this.conn.CreateParameter("@Attributes_Value", DbType.String, AttributesValue),
				this.conn.CreateParameter("@Id", DbType.Int32, Id),
				this.conn.CreateParameter("@Attributes_Methods", DbType.String, AttributesMethods),
				this.conn.CreateParameter("@Attributes_Select", DbType.String, AttributesSelect)
			};
			return this.conn.Execute("UPDATE [HLShop_ProductAttributes] SET [Attributes_Methods]=@Attributes_Methods, [Attributes_Select]=@Attributes_Select,[Attributes_Name] = @Attributes_Name, [Attributes_Value] = @Attributes_Value WHERE (([Id] = @Id))", parameters) >= 1;
		}
		public bool UpdateType(string newAttributesType, string oldAttributesType)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@newAttributesType", DbType.String, newAttributesType),
				this.conn.CreateParameter("@oldAttributesType", DbType.String, oldAttributesType)
			};
			return this.conn.Execute("UPDATE [HLShop_ProductAttributes] SET [Attributes_Type] = @newAttributesType WHERE [Attributes_Type] = @oldAttributesType", parameters) >= 1;
		}
		public bool AddAttributes(string AttributesType, string AttributesName, string AttributesValue, string AttributesMethods, string AttributesSelect)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Attributes_Type", DbType.String, AttributesType),
				this.conn.CreateParameter("@Attributes_Name", DbType.String, AttributesName),
				this.conn.CreateParameter("@Attributes_Value", DbType.String, AttributesValue),
				this.conn.CreateParameter("@Attributes_Methods", DbType.String, AttributesMethods),
				this.conn.CreateParameter("@Attributes_Select", DbType.String, AttributesSelect)
			};
			return this.conn.Execute("INSERT INTO [HLShop_ProductAttributes] ([Attributes_Type], [Attributes_Name], [Attributes_Value],[Attributes_Methods],[Attributes_Select]) VALUES (@Attributes_Type, @Attributes_Name, @Attributes_Value, @Attributes_Methods, @Attributes_Select)", parameters) >= 1;
		}
		public bool GetAddtype_spec(string typename, int sp_id)
		{
			string commandText = "INSERT INTO HLShop_Type_Spec(Attributes_Type,Sp_id)VALUES(@Attributes_Type,@Sp_id)";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Attributes_Type", DbType.String, typename),
				this.conn.CreateParameter("@Sp_id", DbType.Int32, sp_id)
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
		public bool Getdeletetype_spec(string typename, int sp_id)
		{
			return this.conn.Execute(string.Concat(new object[]
			{
				"delete from HLShop_Type_Spec where Attributes_Type='",
				typename,
				"' and Sp_id=",
				sp_id
			})) == 1;
		}
		public bool DeleteAttributes(int Id, string AttributesType)
		{
			string text = "DELETE FROM [HLShop_ProductAttributes] WHERE ";
			if (Id != 0)
			{
				object obj = text;
				text = string.Concat(new object[]
				{
					obj,
					" (Id = ",
					Id,
					")"
				});
			}
			if (AttributesType != "")
			{
				text = text + " (Attributes_Type= '" + AttributesType + "')";
			}
			return this.conn.Execute(text) > 0;
		}
		public DataTable GetAllAttributesType()
		{
			return this.conn.GetDataTable("SELECT Distinct(Attributes_Type) From [HLShop_ProductAttributes]");
		}
		public DataTable GetAttributesByType(string AttributesType)
		{
			return this.conn.GetDataTable("SELECT Distinct(Attributes_Type) From [HLShop_ProductAttributes] WHERE Attributes_Type ='" + AttributesType + "'");
		}
		public DataTable GetAllAttributesTypeAndCount(string AttributesType)
		{
			string text = "SELECT Distinct(Attributes_Type),count(Attributes_Type) as Attributes_Count  From [HLShop_ProductAttributes]  ";
			if (AttributesType != "")
			{
				text = text + "WHERE (Attributes_Type LIKE '%" + AttributesType + "%')";
			}
			text += "GROUP BY Attributes_Type";
			return this.conn.GetDataTable(text);
		}
		public DataTable GetAttributesNameByAttributesType(string AttributesType)
		{
			string text = "SELECT [Id], [Attributes_Type],[Attributes_Name],[Attributes_Value],[Attributes_Sort],[Attributes_State]  ";
			text += ",case when [Attributes_Methods] =0  then '手工录入' when [Attributes_Methods]=1  then '从列表中选择'  ";
			text += "when [Attributes_Methods]=2  then '多行文本框' end as 'Attributes_Methods'";
			text += ",case when [Attributes_Select] =0  then '唯一属性' when [Attributes_Select]=1  then '单选属性'  ";
			text += "when [Attributes_Select]=2  then '复选属性' end as 'Attributes_Select' FROM [HLShop_ProductAttributes]  ";
			if (AttributesType != "")
			{
				text = text + "WHERE (Attributes_Type= '" + AttributesType + "') ";
			}
			text += "ORDER BY Attributes_Sort";
			return this.conn.GetDataTable(text);
		}
		public DataTable GetAttributesById(int Id)
		{
			string sql = "SELECT * FROM HLShop_ProductAttributes  WHERE (Id= " + Id + ")";
			return this.conn.GetDataTable(sql);
		}
		public DataTable GetType_SpecByTypeName(string TypeName, int Sp_id)
		{
			DataTable result;
			try
			{
				DataTable dataTable = this.conn.GetDataTable(string.Concat(new object[]
				{
					"select * from HLShop_Type_Spec where Attributes_Type= '",
					TypeName,
					"' and Sp_id=",
					Sp_id
				}));
				result = dataTable;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}
	}
}
